﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrawlerCore;
using HtmlAgilityPack;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using WCrawlerLib;
using System.Data.Linq.SqlClient;
namespace WCrawler.Controls
{
    public partial class ucForumTopics : System.Web.UI.UserControl
    {
        public void ucForumControlSelectedIndexChanged()
        {
            // This code will Fire from the GridView's SelectedIndexChanged in the UserControl.
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            //Synchronize threads for a forum
            ForumTopic objForumTopic = new ForumTopic();
            objForumTopic.SychronizeTopics(objForum.ForumId);

            ForumTemplateDetail objForumTemplateDetail = new ForumTemplateDetail();
            txtXPath.Text = objForumTemplateDetail.GetSearchThreadsPattern_Params(objForum.ForumId, objForum.ForumTypeId);
            gridViewForumTopic.DataBind();
            if (objForum != null)
            {
                lblForum.Text = objForum.ForumUrl;
                string key = "myTable" + objForum.ForumId.ToString();
                DataTable _myTable = CreateDataTable();
                if (Session[key] != null)
                    _myTable = (DataTable)Session[key];
                gridViewSearchTopic.DataSource = _myTable;
                gridViewSearchTopic.DataBind();

                //Reset message
                lblMessage.Text = "";
            }
        }

        public void ucTreeViewTopic_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ucTreeViewTopic.SelectedNode != null)
            {
                Session["queryNodes"] = ucTreeViewTopic.GetTopics(ucTreeViewTopic.SelectedNode);
                gridViewForumTopic.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Panel pnlCommand = (Panel)ucForumControl.FindControl("pnlCommand");
                //pnlCommand.Visible = false;
                //GridView gridViewForums = (GridView)ucForumControl.FindControl("gridViewForums");
                //gridViewForums.SelectedIndex = 0;
                //CommandField cmdField = (CommandField)gridViewForums.Columns[0];
                //cmdField.ShowEditButton = true;
                //cmdField.ShowDeleteButton = false;
            }
        }

        public DataTable CreateDataTable()
        {
            DataTable myTable;

            // Create a new DataTable.
            myTable = new DataTable("SearchThread");

            // Create DataColumn objects of data types.
            DataColumn colId = new DataColumn("Id");
            colId.DataType = System.Type.GetType("System.Int32");
            myTable.Columns.Add(colId);

            DataColumn colUrl = new DataColumn("Url");
            colUrl.DataType = System.Type.GetType("System.String");
            myTable.Columns.Add(colUrl);

            DataColumn colName = new DataColumn("Topic");
            colName.DataType = System.Type.GetType("System.String");
            myTable.Columns.Add(colName);

            DataColumn colfValue = new DataColumn("fValue");
            colfValue.DataType = System.Type.GetType("System.String");
            myTable.Columns.Add(colfValue);

            DataColumn colThread = new DataColumn("Thread");
            colThread.DataType = System.Type.GetType("System.String");
            myTable.Columns.Add(colThread);
            return myTable;
        }

        protected void btnTestXPath_Click(object sender, EventArgs e)
        {
            //Save into session
            Uri url = new Uri(Request.Url, Request.ApplicationPath);
            GridView gridViewForums = (GridView)Session["ucForum_GridViewForums"];
            Response.Redirect(url + "/TestPages/frmTestXPath.aspx?ForumId=" + (int)gridViewForums.SelectedDataKey["ForumId"]);
        }

        public Forum GetForumSession(int forumId)
        {
            Forum objForum;
            //Get obj _forum
            if (Session[forumId.ToString()] != null)
            {
                objForum = (Forum)Session[forumId.ToString()];
            }
            else
            {
                objForum = new Forum();
                objForum = objForum.GetForum(forumId);
                //objForum.LoadForum();
                Session[forumId.ToString()] = objForum;
            }
            return objForum;
        }

        protected void btnUpdateThreads_Click(object sender, EventArgs e)
        {
            try
            {
                GridView gridViewForums = (GridView)Session["ucForum_GridViewForums"];
                Forum objForum = GetForumSession((int)gridViewForums.SelectedDataKey["ForumId"]);

                Parallel.ForEach<GridViewRow>(gridViewSearchTopic.Rows.OfType<GridViewRow>(), di =>
                {
                    Label lblFValue = (Label)di.FindControl("lblFValue");
                    if (!string.IsNullOrEmpty(lblFValue.Text))
                    {
                        DropDownList dropDownListThread = (DropDownList)di.FindControl("dropDownListThread");
                        HyperLink topic = ((HyperLink)di.FindControl("hyperLinkTopic"));
                        int? fValue = int.Parse(lblFValue.Text);
                        if (dropDownListThread.SelectedIndex != 0)
                        {
                            ForumTopic obj = new ForumTopic();
                            obj.UpdateTopics(objForum.ForumId, int.Parse(dropDownListThread.SelectedValue), topic.NavigateUrl, topic.Text, fValue);
                        }
                    }
                });

                ucTreeViewTopic_SelectedIndexChanged(null, null);
            }
            catch (AggregateException aggEx)
            {
                foreach (Exception ex in aggEx.InnerExceptions)
                {
                    Response.Write("Caught exception" + ex.Message);
                }
            }
        }

        protected void gridViewSearchTopic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList dropDownListTopic = (DropDownList)e.Row.FindControl("dropDownListTopic");
                dropDownListTopic.Items.Insert(0, new ListItem());
                dropDownListTopic.DataBind();
                //Disable DropDownList if fvalue is empty
                Label lblFValue = (Label)e.Row.FindControl("lblFValue");
                if (string.IsNullOrEmpty(lblFValue.Text))
                    dropDownListTopic.Visible = false;
            }
        }

        protected void ForumThreadDetailsLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            int forumId = -1;
            if (Session["ucForum_SelectedValue"] != null)
            {
                forumId = (int)Session["ucForum_SelectedValue"];
            }

            if (Session["queryNodes"] != null)
            {
                ForumTopic obj = new ForumTopic();
                e.Result = obj.GetTopicsByQuery(forumId, Session["queryNodes"].ToString());
            }
            else
            {
                ForumTopic obj = new ForumTopic();
                e.Result = obj.GetTopicsByForum(forumId);
            }
        }

        protected void gridViewForumTopic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int forumId = (int)gridViewForumTopic.DataKeys[e.Row.RowIndex].Values["ForumId"];
                int threadId = (int)gridViewForumTopic.DataKeys[e.Row.RowIndex].Values["TopicId"];
                //Get ForumTopic object by forumid and threadid
                ForumTopic objForumTopic = new ForumTopic();
                objForumTopic = objForumTopic.GetForumTopic(forumId, threadId);

                Label lblThread = (Label)e.Row.FindControl("lblThread");
                if (!string.IsNullOrEmpty(objForumTopic.ForumTopicUrl))
                {
                    HyperLink hyperLinkTopic = (HyperLink)e.Row.FindControl("hyperLinkTopic");
                    hyperLinkTopic.NavigateUrl = GeneralClass.GetFullUrl(objForumTopic.Forum.ForumUrl, objForumTopic.ForumTopicUrl);
                }

                if (gridViewForumTopic.DataKeys[e.Row.RowIndex].Values[1].ToString() == ucTreeViewTopic.SelectedNode.Value
                    || ucTreeViewTopic.SelectedNode.Value == "0")
                {
                    lblThread.Text = lblThread.Text;
                }
                else
                {
                    lblThread.Text = " ......  " + lblThread.Text;
                }
            }
        }

        protected void chkViewEdit_CheckedChanged(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            gridViewForumTopic.DataBind();
        }

        protected void btnSubmitChanges_Click(object sender, EventArgs e)
        {
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            foreach (GridViewRow row in gridViewForumTopic.Rows)
            {
                //Get controls
                Label lblThread = (Label)row.FindControl("lblThread");
                //DropDownList dropDownListParentThread = (DropDownList)row.FindControl("ParentThreadDropDownList");
                //HyperLink hyperLinkTopic = (HyperLink)row.FindControl("hyperLinkTopic");
                TextBox txtTopicName = (TextBox)row.FindControl("txtTopicName");
                TextBox txtTopicUrl = (TextBox)row.FindControl("txtTopicUrl");
                TextBox txtFValue = (TextBox)row.FindControl("txtFValue");
                TextBox txtPrefix = (TextBox)row.FindControl("txtPrefix");

                //int forumId = (int)gridViewForumTopic.DataKeys[row.RowIndex].Values["ForumId"];
                int threadId = (int)gridViewForumTopic.DataKeys[row.RowIndex].Values["TopicId"];
                //Get ForumTopic object by forumid and threadid
                ForumTopic objForumTopic = new ForumTopic();
                objForumTopic = objForumTopic.GetForumTopic(objForum.ForumId, threadId);
                objForumTopic.ForumTopicName = txtTopicName.Text;
                objForumTopic.ForumTopicUrl = txtTopicUrl.Text.Replace(objForum.ForumUrl + "/", "");

                if (!string.IsNullOrEmpty(txtFValue.Text))
                    objForumTopic.fValue = int.Parse(txtFValue.Text);
                else
                {
                    objForumTopic.fValue = null;
                    objForumTopic.ForumTopicName = "";
                    objForumTopic.ForumTopicUrl = "";
                }

                if (!string.IsNullOrEmpty(txtPrefix.Text))
                    objForumTopic.Prefix = txtPrefix.Text;

                objForumTopic.UpdateTopics(objForumTopic);
            }
            lblMessage.Text = "Update successful";
            chkViewEdit.Checked = false;
            gridViewForumTopic.DataBind();
        }

        protected void btnAddBlackListTitle_Click(object sender, EventArgs e)
        {
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            TemplateDetail obj = new TemplateDetail();
            obj.UpdateBlackListTitle(txtBlackListTitle.Text);
        }

        /// <summary>
        /// Get id of rows contains topic title in blacklist
        /// </summary>
        /// <returns></returns>
        public EnumerableRowCollection<int> GetBlackListRowId(DataTable dbForumTopic, string blackListTopic)
        {
            List<string> blackListTopicList = blackListTopic.Split(new char[] { '|' }).ToList();
            //Create table with filter blacklist
            DataTable filterTable = CreateDataTable();
            List<int> listIds = new List<int>();
            //for each blacklist item
            EnumerableRowCollection<DataRow> query;
            foreach (string item in blackListTopicList)
            {
                //get topics contains blacklist item
                query = from row in dbForumTopic.AsEnumerable()
                        where row.Field<string>("Topic").ToLower().Trim().Contains(item)
                        select row;
                if (query.Count() > 0)
                {
                    //add topics in filter table
                    foreach (DataRow r in query)
                    {
                        if (!listIds.Contains(r.Field<int>("Id")))
                        {
                            listIds.Add(r.Field<int>("Id"));
                            filterTable.Rows.Add(r.ItemArray);
                        }
                    }
                }
            }

            //Get blacklist ids in filter table
            EnumerableRowCollection<int> blackListIds = from row in filterTable.AsEnumerable()
                                                        select row.Field<int>("Id");


            return blackListIds;
        }

        protected void DropDownListFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            if (Session["myTable" + objForum.ForumId] != null)
            {
                DataTable dbForumTopic = (DataTable)Session["myTable" + objForum.ForumId];
                //Get Topics in blacklist
                ForumTemplateDetail objForumTemplateDetail = new ForumTemplateDetail();
                string blackListTopic = objForumTemplateDetail.GetBlackListTopic(objForum.ForumId, objForum.ForumTypeId);
                EnumerableRowCollection<int> blackListIds = GetBlackListRowId(dbForumTopic, blackListTopic);
                EnumerableRowCollection<DataRow> query;
                //start filter
                if (DropDownListFilter.SelectedIndex == 0) //Selected Url
                {
                    query = from row in dbForumTopic.AsEnumerable()
                            where !blackListIds.Contains(row.Field<int>("Id"))
                            select row;
                }
                else
                {
                    query = from row in dbForumTopic.AsEnumerable()
                            where blackListIds.Contains(row.Field<int>("Id"))
                            select row;
                }
                gridViewSearchTopic.DataSource = query.AsDataView();
                gridViewSearchTopic.DataBind();
            }
        }

        /// <summary>
        /// Get f value for all links
        /// </summary>
        /// <param name="forumUrl"></param>
        /// <param name="links"></param>
        /// <returns></returns>
        public DataTable SearchFValues(Forum objForum, DataTable dbForumTopic, List<int> blackListIds)
        {
            try
            {
                string pattern = objForum.Browser.FValue_Pattern();
                Parallel.ForEach<DataRow>(dbForumTopic.AsEnumerable(), row =>
                {
                    if (!blackListIds.Contains((int)row["Id"]))
                    {
                        string fullUrl = GeneralClass.GetFullUrl(objForum.ForumUrl, row["Url"].ToString());
                        row["fValue"] = objForum.Browser.FindNewThreadValue(fullUrl, pattern);
                    }
                });
            }
            catch (AggregateException aggEx)
            {
                foreach (Exception ex in aggEx.InnerExceptions)
                {
                    string value = "Caught exception" + ex.Message;
                }
            }
            return dbForumTopic;
        }

        private void SearchThreads(GridView gridViewForums, GridViewRow di, string xpath)
        {
            Forum objForum = GetForumSession((int)gridViewForums.DataKeys[di.RowIndex].Value);
            objForum.LoadForum();
            ForumTemplateDetail objForumTemplate = new ForumTemplateDetail();

            if (string.IsNullOrEmpty(xpath))
                xpath = objForumTemplate.GetSearchThreadsPattern_Params(objForum.ForumId, objForum.ForumTypeId);

            if (!xpath.Contains("//a"))
            {
                //Exception ex = new Exception("ERROR: XPATH MUST GET LINKS");
                //ex.Source = "SearchThreads";
                //Response.Write(ex.Message);
                txtXPath.Text += "ERROR: XPATH MUST GET LINKS";
                return;
            }

            string blackListUrl = objForumTemplate.GetBlackList_Pattern(objForum.ForumId, objForum.ForumTypeId);
            Dictionary<string, string> links = objForum.Browser.SearchTopics(objForum.Browser.HtmlDoc, xpath, blackListUrl);
            if (links.Count() == 0)
            {
                links = objForum.Browser.HtmlDoc.GetAllLinks(objForum.ForumUrl, blackListUrl);
                txtXPath.Text = "Get all links";
            }

            DataTable dbForumTopic = CreateDataTable();
            int id = 1;
            foreach (KeyValuePair<string, string> value in links)
            {
                DataRow myNewRow = dbForumTopic.NewRow();
                myNewRow["Id"] = id++;
                myNewRow["Url"] = GeneralClass.GetFullUrl(objForum.ForumUrl, value.Key);
                myNewRow["Topic"] = value.Value;
                myNewRow["fValue"] = "";
                dbForumTopic.Rows.Add(myNewRow);
            }

            string blackListTopic = objForumTemplate.GetBlackListTopic(objForum.ForumId, objForum.ForumTypeId);
            EnumerableRowCollection<int> blackListIds = GetBlackListRowId(dbForumTopic, blackListTopic);
            //Search f value of new threads
            dbForumTopic = SearchFValues(objForum, dbForumTopic, blackListIds.ToList());

            //Save in session
            Session["myTable" + objForum.ForumId] = dbForumTopic;
        }

        //protected void btnSearchThreads_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        GridView gridViewForums = (GridView)ucForumControl.FindControl("gridViewForums");
        //        Parallel.ForEach<GridViewRow>(gridViewForums.Rows.OfType<GridViewRow>(), di =>
        //        {
        //            CheckBox chkBx = (CheckBox)di.FindControl("chkCheckItem");
        //            if (chkBx != null && chkBx.Checked)
        //            {
        //                Forum objForum = GetForumSession((int)gridViewForums.DataKeys[di.RowIndex].Value);
        //                objForum.LoadForum();
        //                ForumTemplateDetail objForumTemplate = new ForumTemplateDetail();
        //                string xpath = objForumTemplate.GetSearchThreadsPattern_Params(objForum.ForumId, objForum.ForumTypeId);
        //                if (!xpath.Contains("//a"))
        //                {
        //                    Exception ex = new Exception("ERROR: XPATH MUST GET LINKS");
        //                    ex.Source = "SearchThreads";
        //                    Response.Write(ex.Message);
        //                    return;
        //                }
        //                Dictionary<string, string> links = objForum.Browser.SearchThreads(objForum.Browser.HtmlDoc, xpath);
        //                if (links.Count() == 0)
        //                {
        //                    string blackListUrl = objForumTemplate.GetBlackList_Pattern(objForum.ForumId, objForum.ForumTypeId);
        //                    links = objForum.Browser.GetAllLinks(objForum.ForumUrl, objForum.Browser.HtmlDoc, blackListUrl);
        //                    txtXPath.Text = "Get all links";
        //                }
        //                //Search f value of new threads
        //                Dictionary<string, string> results = objForum.Browser.SearchFValues(objForum.ForumUrl, links);

        //                DataTable _myTable = CreateDataTable();
        //                int id = 1;
        //                string url = "";
        //                foreach (KeyValuePair<string, string> value in results)
        //                {
        //                    DataRow myNewRow = _myTable.NewRow();
        //                    myNewRow["Id"] = id++;
        //                    url = value.Key;
        //                    url = GeneralClass.GetFullUrl(objForum.ForumUrl, url);
        //                    myNewRow["Url"] = url;
        //                    myNewRow["Topic"] = links[value.Key];
        //                    myNewRow["fValue"] = value.Value;
        //                    _myTable.Rows.Add(myNewRow);
        //                }
        //                Session["myTable" + objForum.ForumId] = _myTable;
        //            }
        //        });

        //        if (gridViewForums.SelectedDataKey["ForumId"] != null)
        //        {
        //            string key = "myTable" + gridViewForums.SelectedDataKey["ForumId"].ToString();
        //            DataTable _myTable = (DataTable)Session[key];
        //            gridViewSearchTopic.DataSource = _myTable;
        //            gridViewSearchTopic.DataBind();
        //        }
        //        DropDownListFilter_SelectedIndexChanged(null, null);
        //    }
        //    catch (AggregateException aggEx)
        //    {
        //        foreach (Exception ex in aggEx.InnerExceptions)
        //        {
        //            Response.Write("Caught exception" + ex.Message);
        //        }
        //    }
        //    catch (HtmlWebException we)
        //    {
        //        Response.Write(we.Message);
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex.Message);
        //    }
        //}

        protected void btnSearchThreads_Click(object sender, EventArgs e)
        {
            try
            {
                GridView gridViewForums = (GridView)Session["ucForum_GridViewForums"];
                Parallel.ForEach<GridViewRow>(gridViewForums.Rows.OfType<GridViewRow>(), di =>
                {
                    CheckBox chkBx = (CheckBox)di.FindControl("chkCheckItem");
                    if (chkBx != null && chkBx.Checked)
                    {
                        SearchThreads(gridViewForums, di, null);
                    }
                });

                if (gridViewForums.SelectedDataKey["ForumId"] != null)
                {
                    string key = "myTable" + gridViewForums.SelectedDataKey["ForumId"].ToString();
                    DataTable _myTable = (DataTable)Session[key];
                    gridViewSearchTopic.DataSource = _myTable;
                    gridViewSearchTopic.DataBind();
                }
                DropDownListFilter_SelectedIndexChanged(null, null);
            }
            catch (AggregateException aggEx)
            {
                foreach (Exception ex in aggEx.InnerExceptions)
                {
                    Response.Write("Caught exception" + ex.Message);
                }
            }
            catch (HtmlWebException we)
            {
                Response.Write(we.Message);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnFindF_Click(object sender, EventArgs e)
        {
            //if (Session["myTable" + _forum.ForumId] != null)
            //{
            //    //Get obj _forum
            //    GetForumSession();
            //    Dictionary<string, string> links = new Dictionary<string, string>();
            //    Parallel.ForEach<GridViewRow>(gridViewSearchTopic.Rows.OfType<GridViewRow>(), di =>
            //    {
            //        CheckBox chkBx = (CheckBox)di.FindControl("chkCheckItem");
            //        if (chkBx != null && chkBx.Checked)
            //        {
            //            //objForum.CheckForumInfo((int)gridViewForums.DataKeys[di.RowIndex].Value);
            //            Label lblUrl = (Label)di.FindControl("lblUrl");
            //            Label lblTopic = (Label)di.FindControl("lblTopic");
            //            links.Add(lblUrl.Text, lblTopic.Text);
            //        }
            //    });

            //    //Search f value of new threads
            //    Dictionary<string, string> results = _forum.Browser.SearchNewThreadValues(_forum.ForumUrl, links);

            //    _myTable = (DataTable)Session["myTable" + _forum.ForumId];
            //    foreach (DataRow r in _myTable.Rows)
            //    {
            //        string url = r["Url"].ToString();
            //        if (results.Keys.Contains(url))
            //            r["fValue"] = results[url];
            //    }
            //    Session["myTable" + _forum.ForumId] = _myTable;
            //    gridViewSearchTopic.DataSource = _myTable;
            //    gridViewSearchTopic.DataBind();
            //}
        }

        protected void btnSearchBySelectedThread_Click(object sender, EventArgs e)
        {
            try
            {
                GridView gridViewForums = (GridView)Session["ucForum_GridViewForums"];
                if (gridViewForums.SelectedRow != null)
                {
                    GridViewRow di = gridViewForums.SelectedRow;
                    SearchThreads(gridViewForums, di, txtXPath.Text);

                    if (gridViewForums.SelectedDataKey["ForumId"] != null)
                    {
                        string key = "myTable" + gridViewForums.SelectedDataKey["ForumId"].ToString();
                        DataTable _myTable = (DataTable)Session[key];
                        gridViewSearchTopic.DataSource = _myTable;
                        gridViewSearchTopic.DataBind();
                    }
                    DropDownListFilter_SelectedIndexChanged(null, null);
                }
            }
            catch (AggregateException aggEx)
            {
                foreach (Exception ex in aggEx.InnerExceptions)
                {
                    Response.Write("Caught exception" + ex.Message);
                }
            }
            catch (HtmlWebException we)
            {
                Response.Write(we.Message);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnGetTopicLink_Click(object sender, EventArgs e)
        {
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            if (objForum.LoadForum() == null)
                return;
            
            
            ForumTemplateDetail objForumTemplateDetail = new ForumTemplateDetail();
            string newThreadUrl = objForumTemplateDetail.GetNewThreadUrl(objForum.ForumId, objForum.ForumTypeId);
            string searchTopicLink_xpath = objForumTemplateDetail.GetSearchTopicUrl_XPath(objForum.ForumId, objForum.ForumTypeId);

            foreach (GridViewRow row in gridViewForumTopic.Rows)
            {
                //Get controls
                Label lblThread = (Label)row.FindControl("lblThread");
                TextBox txtFValue = (TextBox)row.FindControl("txtFValue");
                if (txtFValue.Text != string.Empty)
                {
                    string postNewThreadUrl = GeneralClass.GetFullUrl(objForum.ForumUrl, newThreadUrl.Replace("[fid]", txtFValue.Text));
                    
                    objForum.Browser.Get(postNewThreadUrl);
                    HtmlNodeCollection nodeCollect = objForum.Browser.HtmlDoc.DocumentNode.SelectNodes(searchTopicLink_xpath);

                    int threadId = (int)gridViewForumTopic.DataKeys[row.RowIndex].Values["TopicId"];
                    //Get ForumTopic object by forumid and threadid
                    ForumTopic objForumTopic = new ForumTopic();
                    objForumTopic = objForumTopic.GetForumTopic(objForum.ForumId, threadId);

                    if (nodeCollect != null)
                    {
                        objForumTopic.ForumTopicName = nodeCollect[nodeCollect.Count - 1].InnerHtml;
                        objForumTopic.ForumTopicUrl = nodeCollect[nodeCollect.Count - 1].Attributes["href"].Value;
                    }

                    objForumTopic.UpdateTopics(objForumTopic);
                }
            }
            lblMessage.Text = "Search topic url complete!";
            gridViewForumTopic.DataBind();
        }
    }
}